// pages/wallet/wallet.js
const app = getApp()
const util = require('../../utils/util.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
          pages:1,
          list: [],
          title: "我的金币",//当前页面的标题
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
          this.setData({
                  headerheight: app.globalData.navHeight
          })
          this.request()
  },
        login: function () {
                var that = this
                wx.login({
                        success: function (res) {
                                var code = res.code
                                var data = {
                                        userCode: code,
                                        userImageUrl: "",
                                        userName: ""
                                }
                                util.promotion(app.globalData.appurl +"/qinqiang/user", "POST", data, that.succeed)
                        }
                })
        },
        succeed: function (res) {
                wx.setStorageSync('tekon', res.data.token)
                wx.setStorageSync('userInfo', res.data.user)
                this.request()

        },
  request:function(){
          wx.showNavigationBarLoading()
          wx.showLoading({
                  title: '玩命加载中...',
          })
          var data = {
                  user_id: wx.getStorageSync("userInfo").id,
                  pageIndex: this.data.pages,
                  pageSize: 20,
          }
          var header = {
                  'authorization': wx.getStorageSync("tekon"),
                  'content-type': "application/x-www-form-urlencoded"
          }
          util.formhttp(app.globalData.appurl +"/goldconins/mycount", 'get', data, header, this.stat);
  },
        stat:function(res){
                console.log(res)
                if (res.status == 401) {
                        this.login()
                }
                wx.hideNavigationBarLoading()
                wx.hideLoading()
                var list = [];
                var totalList = [];
                for (var ids in res.data.list) {
                        var detail = res.data.list[ids];
                        var temp = {
                                coins: detail.coins,
                                create_time: detail.create_time,
                                tag_name: detail.tag_name,
                                tag_type: detail.tag_type
                        }
                        list.push(temp)
                }
                totalList = this.data.list.concat(list);
                this.setData({
                        yesterdayCoins: res.data.todayCoins,
                        coins: res.data.coins,
                        in_coins: res.data.in_coins,
                        rmbRat: res.data.rmbRat,
                        yuan: (res.data.coins / res.data.rmbRat).toFixed(2),
                        list: totalList
                })
        },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
onReachBottom: function () {
        var that = this
        var pages = that.data.pages
        pages++
        that.setData({
                pages: pages
        })
        that.request()
},

 withdraw:function(){
          wx.navigateTo({
                  url: '/pages/withdraw/withdraw',
          })
  },
deposit: function () {
        wx.navigateTo({
                url: '/pages/deposit/deposit',
        })
}
})